
package com.compuvest.admin.update;

import com.compuvest.admin.request.MultiPart;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebServlet("/transactions/UploadOrder")
public class UploadOrders extends HttpServlet {

    public static Logger log = LoggerFactory.getLogger(UploadOrders.class);

    private final static String VERSION = "UploadOrders v2.0 2009-28-07";

    private MySQL mysql;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        mysql = (MySQL)config.getServletContext().getAttribute("context.update.mysql");
        if(mysql == null) {
            mysql = new MySQL();
            config.getServletContext().setAttribute("context.update.mysql", mysql);
        }

        log.info("Init: " + VERSION);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        response.setContentType("text/xml;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            if(ServletFileUpload.isMultipartContent(request)) {
                long startEvent = System.currentTimeMillis();

                MultiPart mrequest = new MultiPart(request, 64535); // 65k

                String result = doUploadOrders(mrequest);

                int endEvent = (int)(System.currentTimeMillis() - startEvent);

                StringBuilder xml = new StringBuilder(2000);
                xml.append("<?xml version='1.0' encoding='UTF-8'?>");
                xml.append("<order>");
                xml.append("<sessionid>").append(request.getSession().getId()).append("</sessionid>");
                xml.append("<time>").append(endEvent).append("</time>");
                xml.append(result);
                xml.append("</order>");

                out.print(xml.toString());
            }
            else {
                out.print("<h3>IP Address is [" + request.getRemoteAddr() + "]<br/>This accident send to FBI</h3>");
            }
        }
    }

// if send orderNumber - change status
// without - take orders
//
    protected String doUploadOrders(MultiPart mrequest) {

        String orderNumber = mrequest.getParameter("orderNumber");
        if(orderNumber != null) {
            mysql.setOrderStatus(mrequest);
        }

        return mysql.getOrder(mrequest);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "Update Orders";
    }

    @Override
    public void destroy() {
        mysql = null;

        log.info("Destroy: " + VERSION);
    }// </editor-fold>

}
